package com.keyinong.caizhengting.pay;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.Tools.HttpTools;
import com.keyinong.caizhengting.view.DingcanActivity;
import com.loopj.android.http.JsonHttpResponseHandler;

public class TeamBalancepayActivity extends Activity {
	private TextView tv_price;
	private TextView tv_balance;
	private Button btn_pay;
	private ImageButton ib_fanhui;
	HttpTools httpTools=null;
	private String token;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_team_balancepay);
		initRes();
	}

	private void initRes() {
		tv_price=(TextView)findViewById(R.id.tv_price);
		tv_balance=(TextView)findViewById(R.id.tv_balance);
		btn_pay=(Button)findViewById(R.id.btn_pay);
		ib_fanhui=(ImageButton)findViewById(R.id.ib_fanhui);
		String price = getIntent().getStringExtra("total");		
		tv_price.setText("�??"+price+"�??");
//		tv_price.setText(price);
		btn_pay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Balancepay();
			}
		});
		ib_fanhui.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		Balance();
	}

	private void Balance() {
		SharedPreferences sp=getSharedPreferences("spReg", Context.MODE_PRIVATE);
		token = sp.getString("data", "");
		if (httpTools == null) {
			httpTools = new HttpTools(TeamBalancepayActivity.this);
		}
		httpTools.getBalance(token,new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					boolean code = response.getBoolean("code");
					String msg = response.getString("msg");
					Log.i("zq", "teambalance---"+response.toString());
					if (code == true) {
						String data=response.getString("data");
//						Toast.makeText(BalancepayActivity.this, msg, Toast.LENGTH_SHORT).show();
						//										view.setBackgroundResource(R.drawable.shouchang1);
						tv_balance.setText("�??"+data+"�??");
					} else {
						Toast.makeText(TeamBalancepayActivity.this, msg, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});
	}
	
	private void Balancepay() {
		SharedPreferences sp=getSharedPreferences("spReg", Context.MODE_PRIVATE);
		token = sp.getString("data", "");
		String orderid=getIntent().getStringExtra("orderid2");
		Log.i("zq", "teambalancepayorderid---"+orderid);
		if (httpTools == null) {
			httpTools = new HttpTools(TeamBalancepayActivity.this);
		}
		httpTools.submitpay(token,orderid,new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					boolean code = response.getBoolean("code");
					String msg = response.getString("msg");
					Log.i("zq", "teambalancepay---"+response.toString());
					if (code == true) {
//						String data=response.getString("data");
						Toast.makeText(TeamBalancepayActivity.this, msg, Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(TeamBalancepayActivity.this, DingcanActivity.class);
						startActivity(intent);
						//										view.setBackgroundResource(R.drawable.shouchang1);
//						tv_balance.setText("�??"+data+"�??");
					} else {
						Toast.makeText(TeamBalancepayActivity.this, msg, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});
	}
}
